package com.beyondsoft.mcms.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findMemberInfoesByMemberNameLikeAndBusinessTypeAndPositionType",
		"findMemberInfoesByUsernameEquals", "findMemberInfoesByMemberNameEquals" })
public class MemberInfo {

	/**
     */
	@NotNull
	@ManyToOne
	private Item businessType;

	/**
     */
	@NotNull
	@ManyToOne
	private Item positionType;

	/**
     */
	@NotNull
	@Size(min = 1)
	@Column(unique = true)
	@Pattern(regexp = "^[A-Za-z_0-9]{1,15}$")
	private String username;

	/**
     */
	private String password;

	/**
     */
	@NotNull
	@Size(min = 1)
	@Column(unique = true)
	private String memberName;

	/**
     */
	private String adress;

	/**
     */
	@NotNull
	private String delegateName;

	/**
     */
	@NotNull
	private String delegatePosition;

	/**
     */
	@NotNull
	@Pattern(regexp = "^1[0-9]{10}$")
	private String delegateMobile;

	/**
     */
	private String delegatePhone;

	/**
     */
	private String delegateMicroMessage;

	/**
     */
	@NotNull
	@Pattern(regexp = "^(\\w+\\.)*\\w+@(\\w+\\.)+\\w+$")
	private String delegateEmail;

	/**
     */
	private String contactName;

	/**
     */
	private String contactMobile;

	/**
     */
	private String contactPhone;

	/**
     */
	private String contactMicroMessage;

	/**
     */
	private String contactEmail;

	/**
     */
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "MM")
	private Date rigesterDate;

	/**
     */
	private String createdBy;

	/**
     */
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "MM")
	private Date updateDate;

	/**
     */
	private String updatedBy;

	public static TypedQuery<MemberInfo> findMemberInfoesByBusinessTypeInAndPositionTypeIn(List<Long> businessIds,
			List<Long> positionTypeIds) {
		if ((businessIds == null || businessIds.size() == 0)
				&& (positionTypeIds == null || positionTypeIds.size() == 0)) throw new IllegalArgumentException(
				"The businessIds and positionTypeIds argument is required");

		EntityManager em = MemberInfo.entityManager();
		String sql = "SELECT o FROM MemberInfo AS o WHERE  ";
		if (businessIds != null && businessIds.size() > 0) sql += "o.businessType.id in (:businessIds)  ";
		if (positionTypeIds != null && positionTypeIds.size() > 0) {
			if (businessIds != null && businessIds.size() > 0) sql += " and   ";
			sql += " o.positionType.id in (:positionTypeIds)   ";
		}

		TypedQuery<MemberInfo> q = em.createQuery(sql, MemberInfo.class);
		if (businessIds != null && businessIds.size() > 0) q.setParameter("businessIds", businessIds);
		if (positionTypeIds != null && positionTypeIds.size() > 0) q.setParameter("positionTypeIds", positionTypeIds);
		return q;
	}

	public static TypedQuery<MemberInfo> findMemberInfoesByMemberNameLikeAndBusinessTypeAndPositionType(
			String memberName, Item businessType, Item positionType) {
		if (memberName == null || memberName.length() == 0) memberName = "*";
		memberName = memberName.replace('*', '%');
		if (memberName.charAt(0) != '%') {
			memberName = "%" + memberName;
		}
		if (memberName.charAt(memberName.length() - 1) != '%') {
			memberName = memberName + "%";
		}

		StringBuffer sql = new StringBuffer(
				"SELECT o FROM MemberInfo AS o WHERE LOWER(o.memberName) LIKE LOWER(:memberName) ");

		if (businessType != null) {
			sql.append(" AND o.businessType = :businessType ");
		}
		if (positionType != null) {
			sql.append(" AND o.positionType = :positionType ");
		}
		sql.append(" ORDER BY o.rigesterDate DESC ");

		EntityManager em = MemberInfo.entityManager();
		TypedQuery<MemberInfo> q = em.createQuery(sql.toString(), MemberInfo.class);

		q.setParameter("memberName", memberName);
		if (businessType != null) {
			q.setParameter("businessType", businessType);
		}
		if (positionType != null) {
			q.setParameter("positionType", positionType);
		}

		return q;
	}

	public static TypedQuery<MemberInfo> findMemberInfoesByConference(Long conference) {
		EntityManager em = MemberInfo.entityManager();
		TypedQuery<MemberInfo> q = em.createQuery(
				"SELECT distinct o.memberInfo FROM Advice AS o WHERE o.conference.id = :conference ", MemberInfo.class);
		q.setParameter("conference", conference);
		return q;
	}

}
